package com.woniuxy.warehouse.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.commons.entity.warehouse.IoWarehouseProduct;
import com.woniuxy.commons.entity.warehouse.dto.SaleGoodInfoDto;
import com.woniuxy.commons.result.Result;
import com.woniuxy.warehouse.mapper.IoWarehouseProductDao;
import com.woniuxy.warehouse.service.IoWarehouseProductService;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (IoWarehouseProduct)表服务实现类
 *
 * @author makejava
 * @since 2022-04-20 15:33:50
 */
@Service
public class IoWarehouseProductServiceImpl implements IoWarehouseProductService {
    @Resource
    private IoWarehouseProductDao ioWarehouseProductDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Result queryById(Integer id) {
        return Result.ok(ioWarehouseProductDao.queryById(id));
    }

/**
 * @param ioWarehouseProducts:
 * @return Result
 * @description TODO 批量插入
 */
    @Override
    public Result insertBatch(List<IoWarehouseProduct> ioWarehouseProducts) {
        ioWarehouseProductDao.insertBatch(ioWarehouseProducts);
        return Result.ok();
    }
    /**
     * @param :
     * @return Result
     * @description TODO 查找插入
     */

    @Override
    public Result findSaleGoodInfo(Integer currentPage, Integer pageSize) {
       PageHelper.startPage(currentPage,pageSize);
       List<SaleGoodInfoDto> saleGoodInfos = ioWarehouseProductDao.findSaleGoodInfo();
       PageInfo<SaleGoodInfoDto> of = PageInfo.of(saleGoodInfos);
       Result<PageInfo> listResult = new Result<>();
       if (saleGoodInfos.size()>0){
           listResult.setData(of);
           listResult.setCode(200);
           listResult.setMessage("查询成功");
       }else {
           listResult.setData(null);
           listResult.setCode(500);
           listResult.setMessage("数据为空");
       }
        return listResult;
    }



    /**
     * 新增数据
     *
     * @param ioWarehouseProduct 实例对象
     * @return 实例对象
     */
    @Override
    public Result insert(IoWarehouseProduct ioWarehouseProduct) {
        ioWarehouseProductDao.insert(ioWarehouseProduct);
        return Result.ok();
    }

    /**
     * 修改数据
     *
     * @param ioWarehouseProduct 实例对象
     * @return 实例对象
     */
    @Override
    public Result update(IoWarehouseProduct ioWarehouseProduct) {
        ioWarehouseProductDao.update(ioWarehouseProduct);
        return this.queryById(ioWarehouseProduct.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.ioWarehouseProductDao.deleteById(id) > 0;
    }
/**
 * @param currentPage:
 * @param pageSize:
 * @param ioWarehouseProduct:
 * @return Result
 * @description TODO 分页查找
 */
    @Override
    public Result queryByPage(Integer currentPage, Integer pageSize, IoWarehouseProduct ioWarehouseProduct) {
        PageHelper.startPage(currentPage,pageSize);
        List<IoWarehouseProduct> ioWarehouseProducts = ioWarehouseProductDao.findAll(ioWarehouseProduct);

        PageInfo<IoWarehouseProduct> of = PageInfo.of(ioWarehouseProducts);

        Result<PageInfo> listResult = new Result<>();
        if (ioWarehouseProducts.size()>0){
            listResult.setData(of);
            listResult.setCode(200);
            listResult.setMessage("查询成功");
        }else {
            listResult.setData(null);
            listResult.setCode(500);
            listResult.setMessage("数据为空");
        }
        return listResult;
    }


}
